using System;
using Winhance.Core.Features.Common.Interfaces;

namespace Winhance.Infrastructure.Features.Common.ScriptGeneration
{
    /// <summary>
    /// Provides PowerShell script templates with OS-specific adjustments.
    /// </summary>
    public class PowerShellScriptTemplateProvider : IScriptTemplateProvider
    {
        private readonly ISystemServices _systemService;
        private readonly ILogService _logService;

        /// <summary>
        /// Initializes a new instance of the <see cref="PowerShellScriptTemplateProvider"/> class.
        /// </summary>
        /// <param name="systemService">The system service.</param>
        /// <param name="logService">The logging service.</param>
        public PowerShellScriptTemplateProvider(
            ISystemServices systemService,
            ILogService logService)
        {
            _systemService = systemService ?? throw new ArgumentNullException(nameof(systemService));
            _logService = logService ?? throw new ArgumentNullException(nameof(logService));
        }

        /// <inheritdoc/>
        public string GetPackageRemovalTemplate()
        {
            // Windows 10 compatibility: Don't use -AllUsers with Remove-AppxPackage
            return @"Get-AppxPackage -Name ""{0}"" | Remove-AppxPackage -ErrorAction SilentlyContinue
Get-AppxProvisionedPackage -Online | Where-Object {{ $_.DisplayName -eq ""{0}"" }} | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue";
        }

        /// <inheritdoc/>
        public string GetCapabilityRemovalTemplate()
        {
            return @"Get-WindowsCapability -Online | Where-Object {{ $_.Name -like ""{0}*"" }} | Remove-WindowsCapability -Online";
        }

        /// <inheritdoc/>
        public string GetFeatureRemovalTemplate()
        {
            return @"Write-Host ""Disabling optional feature: {0}"" -ForegroundColor Yellow
Disable-WindowsOptionalFeature -Online -FeatureName ""{0}"" -NoRestart | Out-Null";
        }

        /// <inheritdoc/>
        public string GetRegistrySettingTemplate(bool isDelete)
        {
            if (isDelete)
            {
                return @"reg delete ""{0}"" /v {1} /f | Out-Null";
            }
            else
            {
                return @"reg add ""{0}"" /v {1} /t REG_{2} /d {3} /f | Out-Null";
            }
        }

        /// <inheritdoc/>
        public string GetScriptHeader(string scriptName)
        {
            return $@"# {scriptName}.ps1
# This script removes Windows bloatware apps and prevents them from reinstalling
# Source: Winhance (https://github.com/memstechtips/Winhance)
# Generated by Winhance on {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}

";
        }

        /// <inheritdoc/>
        public string GetScriptFooter()
        {
            return @"
# Prevent apps from reinstalling
reg add ""HKLM\SOFTWARE\Policies\Microsoft\Windows\CloudContent"" /v DisableWindowsConsumerFeatures /t REG_DWORD /d 1 /f | Out-Null

Write-Host ""=== Script Completed ==="" -ForegroundColor Green
";
        }
    }
}